﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;
using System;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/AudioManager")]
    public class AudioManager : MonoBehaviour, ISingleton
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            AudioKit.Config.AudioLoaderPool = new ResKitAudioLoaderPool();
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static AudioManager Instance
        {
            get { return MonoSingletonProperty<AudioManager>.Instance; }
        }

        /// <summary>
        /// 背景音量
        /// </summary>
        public float BGMVolume
        {
            get
            {
                return AudioKit.Settings.MusicVolume.Value;
            }
            set
            {
                AudioKit.Settings.MusicVolume.Value = value;
            }
        }

        /// <summary>
        /// 音效音量
        /// </summary>
        public float SoundVolume
        {
            get
            {
                return AudioKit.Settings.SoundVolume.Value;
            }
            set
            {
                AudioKit.Settings.SoundVolume.Value = value;
            }
        }

        /// <summary>
        /// 设置背景音乐音量
        /// </summary>
        /// <param name="num"></param>
        public void SetBGMVolume()
        {
            if (BGMVolume == 1)
            {
                BGMVolume = 0;
            }
            else
            {
                BGMVolume = 1;
            }
        }

        /// <summary>
        /// 设置音效音量
        /// </summary>
        /// <param name="num"></param>
        public void SetSoundVolume()
        {
            if (SoundVolume == 1)
            {
                SoundVolume = 0;
            }
            else
            {
                SoundVolume = 1;
            }
        }


        /// <summary>
        /// 播放背景音乐
        /// </summary>
        /// <param name="musicName">音效名</param>
        /// <param name="loop">是否循环</param>
        /// <param name="onBeganCallback">开始播放回调</param>
        /// <param name="onEndCallback">播放结束回调</param>
        public void PlayMusic(string musicName, bool loop = true, Action onBeganCallback = null, Action onEndCallback = null)
        {
            AudioKit.PlayMusic(musicName, loop, onBeganCallback, onEndCallback);
        }

        /// <summary>
        /// 停止背景音乐,无法恢复
        /// </summary>
        public void StopMusic()
        {
            AudioKit.StopMusic();
        }

        /// <summary>
        /// 暂停背景音乐
        /// </summary>
        public void PauseMusic()
        {
            AudioKit.PauseMusic();
        }

        /// <summary>
        /// 恢复背景音乐
        /// </summary>
        public void ResumeMusic()
        {
            AudioKit.ResumeMusic();
        }

        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="soundName">音效名</param>
        /// <param name="loop">是否循环</param>
        /// <param name="callBack">播放结束回调</param>
        public AudioPlayer PlaySound(string soundName, bool loop = false, Action<AudioPlayer> callBack = null)
        {
            return AudioKit.PlaySound(soundName, loop, callBack);
        }

        /// <summary>
        /// 暂停所有音效
        /// </summary>
        public void StopAllSound()
        {
            AudioKit.StopAllSound();
        }

        /// <summary>
        /// 停止音效
        /// </summary>
        /// <param name="audioPlayer">音效播放组件</param>
        public void StopSound(AudioPlayer audioPlayer)
        {
            audioPlayer.Stop();
        }
    }
}
